<?php

use console\db\Migration;

/**
 * 创建话题相关信息表
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class m190102_180239_t_topic extends Migration {

    /**
     * {@inheritdoc}
     */
    public function up() {

        /**
         * 话题表
         * @table `%sns_topic`
         */
        $this->tableBuilder([
            'name' => 'sns_topic',
            'comment' => '圈子话题表',
            'columns' => [
                'id' => $this->primaryKey(),
                'name' => $this->string(50)->notNull()->comment('话题名称'),
                'brand' => $this->char(255)->defaultValue('')->comment('图标'),
                'cover' => $this->string(255)->defaultValue('')->comment('封面'),
                'group_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('隶属圈子ID'),
                'user_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('创建人ID'),
                'username' => $this->string(50)->notNull()->comment('创建人用户名'),
                'weight' => $this->tinyint(4)->unsigned()->notNull()->defaultValue(0)->comment('话题权重'),
                'status' => $this->tinyint(1)->notNull()->defaultValue(10)->comment('状态'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
                'deleted_at' => $this->unixTimestamp()->comment('删除时间'),
            ]
        ]);

        /**
         * 话题统计信息表
         * @table `%sns_topic_stats`
         */
        $this->tableBuilder([
            'name' => 'sns_topic_stats',
            'comment' => '话题统计信息表',
            'columns' => [
                'id' => $this->primaryKey(),
                'topic_id' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('圈子ID'),
                'users' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('关注数'),
                'views' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('浏览次数'),
                'dynamics' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('动态数'),
                'comments' => $this->integer(11)->unsigned()->notNull()->defaultValue(0)->comment('评论数'),
                'created_at' => $this->unixTimestamp()->comment('创建时间'),
                'updated_at' => $this->unixTimestamp()->comment('更新时间'),
            ],
            'index' => [
                [
                    'columns' => 'topic_id',
                ],
            ]
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function down() {
        $this->dropTable('{{%sns_topic}}');
        $this->dropTable('{{%sns_topic_stats}}');
    }

}
